package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Tensorflow;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("TensorflowController")
public class TensorflowController extends CmiEmaController {

	@RequestMapping("/listTensorflows")
	public ModelAndView listTensorflows(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = tensorflowPaginationService.updatePagination(request, pagination);
		Set<Tensorflow> tensorflows = tensorflowPaginationService.getPaginationTensorflows(pagination);

		mav.addObject("tensorflows", tensorflows);
		mav.addObject("entityName", "tensorflow");
		mav.addObject("pagination", pagination);
		mav.setViewName("tensorflow/listTensorflows");
		return mav;
	}

	public String listTensorflows() {
		return "redirect:/listTensorflows";
	}

	@RequestMapping("/viewTensorflow")
	public ModelAndView viewTensorflow(@RequestParam Integer tensorflowId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("tensorflow", tensorflowDAO.findTensorflowByPrimaryKey(tensorflowId));
		mav.setViewName("tensorflow/viewTensorflow");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteTensorflows")
	public ModelAndView confirmDeleteTensorflows(
			@RequestParam(value = "deleteTensorflowsIds", required = true) Set<Integer> deleteTensorflowsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Tensorflow> deleteTensorflows = tensorflowDAO.findTensorflowsByIds(deleteTensorflowsIds);
		Set<Tensorflow> referencedTensorflows = tensorflowService.loadReferenceTensorflows(deleteTensorflows);
		boolean hasReferenced;
		if (referencedTensorflows.size() == 0) {
			mav.addObject("deleteEntitys", deleteTensorflows);
			hasReferenced = false;
		} else {
			deleteTensorflows.removeAll(referencedTensorflows);
			mav.addObject("referencedEntitys", referencedTensorflows);
			mav.addObject("deleteEntitys", deleteTensorflows);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Tensorflow");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listTensorflows");
		return mav;
	}

	@RequestMapping("/deleteTensorflow")
	public String deleteTensorflow(@RequestParam Integer tensorflowId) {
		Tensorflow tensorflow = tensorflowDAO.findTensorflowByPrimaryKey(tensorflowId);
		tensorflowService.deleteTensorflow(tensorflow);
		return "forward:/listTensorflows";
	}

	@RequestMapping("/confirmDeleteTensorflow")
	public ModelAndView confirmDeleteTensorflow(@RequestParam Integer tensorflowId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("tensorflow", tensorflowDAO.findTensorflowByPrimaryKey(tensorflowId));

		mav.setViewName("tensorflow/deleteTensorflow");

		return mav;
	}

	@RequestMapping("/deleteTensorflows")
	public String deleteTensorflows(
			@RequestParam(value = "deleteTensorflowsId", required = true) Set<Integer> deleteTensorflowsId) {
		tensorflowService.deleteTensorflows(deleteTensorflowsId);
		return "forward:/listTensorflows";
	}

	@RequestMapping("/editTensorflow")
	public ModelAndView editTensorflow(@RequestParam Integer tensorflowId) {

		ModelAndView mav = new ModelAndView();

		Tensorflow tensorflow = tensorflowDAO.findTensorflowByPrimaryKey(tensorflowId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("tensorflow", tensorflow);

		// 把所有的引用tensorflow对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(tensorflowService.loadTensorflowObjectReferencedSet(tensorflow));

		// 将编辑页面tensorflow添加到mav中
		mav.setViewName("tensorflow/editTensorflow");
		return mav;
	}

	@RequestMapping("/newTensorflow")
	public ModelAndView newTensorflow() {
		ModelAndView mav = new ModelAndView();
		Tensorflow tensorflow = new Tensorflow();

		mav.addObject("tensorflow", tensorflow);

		// 把所有的引用tensorflow对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(tensorflowService.loadTensorflowObjectReferencedSet(tensorflow));

		mav.addObject("newFlag", true);
		mav.setViewName("tensorflow/editTensorflow");

		return mav;
	}

	@RequestMapping("/saveTensorflow")
	public ModelAndView saveTensorflow(@ModelAttribute Tensorflow tensorflow) {

		ModelAndView mav = new ModelAndView();

		Tensorflow existingTensorflow = CmiHibernateProxyUtil
				.deproxy(tensorflowDAO.findTensorflowByPhysicalPrimaryKey(tensorflow.getName()));

		// 新建记录
		if (tensorflow.getId() == null) {
			if (tensorflow.equals(existingTensorflow)) {

				mav.addAllObjects(tensorflowService.loadTensorflowClassReferencedSet(tensorflow));
				// mav = editContinue(mav, tensorflow);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("tensorflow/editTensorflow");
			} else {
				tensorflowService.saveTensorflow(tensorflow);
				mav.setViewName("forward:/listTensorflows");
			}
		}
		// 编辑记录
		else {
			Tensorflow originTensorflow = CmiHibernateProxyUtil
					.deproxy(tensorflowDAO.findTensorflowByPrimaryKey(tensorflow.getId()));
			if (!tensorflow.equals(originTensorflow) && tensorflow.equals(existingTensorflow)) {
				mav.addAllObjects(tensorflowService.loadTensorflowClassReferencedSet(tensorflow));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("tensorflow/editTensorflow");
			} else {
				tensorflowService.saveTensorflow(tensorflow);
				mav.setViewName("forward:/listTensorflows");
			}
		}
		return mav;
	}

	@RequestMapping("/tensorflowController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
